var publicJs = require("../../../../public.js");

Page({
  data: {
    realName: '',
    idcard: '',
    vocation: '我是上班族',
    items: [
      { value: '我是上班族', name: '我是上班族', checked: true },
      { value: '我是个体户', name: '我是个体户' },
    ],
    aptitude: '已有信用卡',
    items2: [
      { value: '已有信用卡', name: '已有信用卡', checked: true },
      { value: '暂无信用卡', name: '暂无信用卡' },
    ]
  },
  onLoad: function (options) {
    this.setData({
      type: options.type,
      money: options.money,
      cycle: options.cycle,
      purpose: options.purpose,
      repayment: options.repayment
    })
  }, 
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    this.data.vocation = e.detail.value;
    var items = this.data.items;
    for (var i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value == e.detail.value
    }

    this.setData({
      items: items
    });
  },
  radioChange2: function (e) {
    console.log('radio2发生change事件，携带value值为：', e.detail.value);
    this.data.aptitude = e.detail.value;
    var items2 = this.data.items2;
    for (var i = 0, len = items2.length; i < len; ++i) {
      items2[i].checked = items2[i].value == e.detail.value
    }

    this.setData({
      items2: items2
    });
  },
  realNameChange: function (e) {
    this.data.realName = e.detail.value
  },
  idcardChange: function (e) {
    this.data.idcard = e.detail.value
  },
  doLoan: function (e) {
    var that = this;
    if (this.data.realName == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的真实姓名!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }

    if (this.data.idcard == '') {
      wx.showModal({
        title: '提示',
        content: '请输入您的身份证号!',
        showCancel: false,
        success: function (res) { }
      });
      return false;
    }
    publicJs.JsonPost("Borrow/save", {
      money: parseInt(this.data.money),
      vocation: this.data.vocation,
      aptitude: this.data.aptitude,
      real_name: this.data.realName,
      cycle: this.data.cycle,
      repayment: this.data.repayment,
      idcard: this.data.idcard,
      type: parseInt(this.data.type)
    }, function (data) {
      publicJs.showLoading("正在提交...请稍后...");
      console.log(data);

      if (data !== false) {
        publicJs.closeLoading();
        wx.showModal({
          title: '提示',
          content: '提交成功!',
          showCancel: false,
          success: function (res) {
            that.setData({
              realName: '',
              idcard: '',
              vocation: '我是上班族',
              items: [
                { value: '我是上班族', name: '我是上班族', checked: true },
                { value: '我是个体户', name: '我是个体户' },
              ],
              aptitude: '已有信用卡',
              items2: [
                { value: '已有信用卡', name: '已有信用卡', checked: true },
                { value: '暂无信用卡', name: '暂无信用卡' },
              ]
            })
          }
        });
      }

    })
  }
})